/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.document.request;

import org.dopdf.document.model.PageDetail;
import org.dopdf.document.read.PageWiseDocumentViewer;
import org.dopdf.utils.Constants;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * The class that is can process requests for showing a pdf page.  This class renders the required pdf page.
 *
 * @author Deep Shah
 */
public class PDFPageRequestProcessor extends AbstractRegExBasedRequestProcessor {
    /** The logger reference. */
    private static final Logger logger = Logger.getLogger(PDFPageRequestProcessor.class.toString());

    /**
     * {@inheritDoc}
     */
    public void process(final HttpServletRequest request, final HttpServletResponse response) {
        try {
            final int pageNumber = Integer.parseInt(request.getParameter(Constants.PDFPage.PAGE_KEY));
            final PageDetail pageDetail = new PageDetail(request.getParameter(Constants.PDFPage.ID_KEY),
                    request.getParameter(Constants.PDFPage.STRATEGY_KEY),
                    pageNumber,
                            request.getParameter(Constants.PDFPage.SIZE_KEY));
            new PageWiseDocumentViewer().renderPage(pageDetail, response);
            logger.finest("Finished rendering of page number: " + pageNumber);
        } catch (final Exception e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getRegEx() {
        return Constants.Resource.PDF_PAGE_RESOURCE_PATTERN;
    }
}
